$(document).ready(function() {

    showMoreInfo($("#div_"), "imgInfo");

});


var SELECT_EC_CANDIDATES_ID = "ecMemberCandidatesId";
var selectedElement = undefined;

function searchSiteStaff(element, url){
    selectedElement = element;
    var name = selectedElement.val();
    windowCaller("Select Site Staff", url, { "nameOrSurname": name });
}

function closeWindowsSiteStaff(id,nombre){
    $.window.closeAll();

    /*este elemento mostrara el nombre del staff seleccionado*/
    selectedElement.val(nombre);

    /*este elemento llevara al servidor el id del staff seleccionado*/
    selectedElement.prev().val(id);
}

function deleteRowSS(element) {

    if($('.siteStaffClass').length>1){
        element.remove();
    }else{
        element.children(3).children(0).val('');
        element.children(3).children(1).val('');
    }

    repopulateECCandidates();
}


function addRowSiteStaff(element) {

    var counts = ++contadorParaNuevasFilas;


    var rowContact = element.clone()[0];
    $(rowContact.children[0].children[1].children[0]).val('-1');
    rowContact.children[0].children[1].children[0].name = 'siteStaff['+counts+'].role.id';

    rowContact.children[1].children[1].children[0].name = 'siteStaff['+counts+'].otherRole';
    rowContact.children[1].children[1].children[0].value = '';
    $(rowContact.children[1]).hide();

    rowContact.children[2].children[1].children[0].value = '';
    rowContact.children[2].children[1].children[0].name = 'siteStaff['+counts+'].involucrado.id';
    rowContact.children[2].children[1].children[1].value = '';
    rowContact.children[2].children[1].children[1].value = '';
    rowContact.children[3].children[1].children[0].checked = false;
    rowContact.children[3].children[2].children[0].onclick = function(){deleteRowSS($(this).parent().parent().parent()); return false;};

    element.after(rowContact);
}

function showHideOtherRole(select){
    var opSelected = $(select).val();
    var td =select.parent().parent().parent().children()[1];
    if(opSelected == "0"){
        $(td).show();
    }else{
        $(td).hide();
    }
}

function repopulateECCandidates(){
    var optionIds = [];
    var piid = $('#piId').val();
    var siid = $('#siId').val();
    if (piid != "" && !isNaN(piid)){
        optionIds.push(piid);
    }
    if (siid != "" && !isNaN(siid)){
        optionIds.push(siid);
    }

    //guardo ids de roles
    $(".siteStaffClass").each(
        function(index, tbody){
            var optionId = tbody.children[2].children[1].children[0].value;
            optionIds.push(optionId);
        }
    )

    //Guardo Choose an Option
    var chooseAnOptionOp = $("#"+SELECT_EC_CANDIDATES_ID+" option[value='-1']")[0];

    //Existe la Opcion seleccionada?
    var selectedOp = undefined;
    if($.inArray($("#"+SELECT_EC_CANDIDATES_ID+" :selected").val(),optionIds) != -1){
        selectedOp = $("#"+SELECT_EC_CANDIDATES_ID+" :selected")[0];
        optionIds.splice(
                        optionIds.indexOf(
                            $("#"+SELECT_EC_CANDIDATES_ID+" :selected").val()
                        ),
                        1
        );
    }

    //Borro todos las options
    $("#"+SELECT_EC_CANDIDATES_ID).empty();

    //escribo Choose an Option
    $("#"+SELECT_EC_CANDIDATES_ID).append(chooseAnOptionOp);

    //escribo Choose an Chosen si existe
    if(selectedOp != undefined){
        $("#"+SELECT_EC_CANDIDATES_ID).append(selectedOp);
    }

    //escribo pi si esta en optionIds
    if($("#searchPI").val() != ""){
        witeOptionIfExistsInArray(optionIds,piid,$("#searchPI").val());
    }

    //escribo si si esta en optionIds
    if($("#searchSI").val() != ""){
        witeOptionIfExistsInArray(optionIds,siid,$("#searchSI").val());
    }

    $(".siteStaffClass").each(
        function(index, tbody){
            var optionId = tbody.children[2].children[1].children[0].value;
            var optionContent = tbody.children[2].children[1].children[1].value;
            if(optionContent != ""){
                witeOptionIfExistsInArray(optionIds,optionId,optionContent);
            }
        }
    )
}

function witeOptionIfExistsInArray(array,elementInArray,optionContent){
    if($.inArray(elementInArray,array) != -1){
        var option = '<option value="'+elementInArray+'">'+optionContent+'</option>'
        $("#"+SELECT_EC_CANDIDATES_ID).append(option);
        array.splice(
            array.indexOf(elementInArray),
            1
        );
    }
}

function showHideECMembers(){
    var opSelected = $("#ECMemberSelect").val();
    var td = "#ECMemberId";
    if(opSelected == "0"){
        $(td).show();
    }else{
        $(td).hide();
        $("#"+SELECT_EC_CANDIDATES_ID).val(-1);
    }
}

function loadOtherSiteStaff(){
    $(".siteStaffClass").each(
        function(index,value){
            var tr = $(value).children()[0];
            var td = $(tr).children()[1];
            var select = $(td).children()[0];
            showHideOtherRole($(select));
            var i= 0;
        }
    )
}